<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\learn\Book;

use app\api\model\learn\Clock as ClockModel;
use app\api\service\User as UserService;
use app\common\model\learn\book\TitleUser as TitleUserModel;
use think\facade\Db;

/**
 * 商品评价模型
 * Class Article
 * @package app\api\model
 */
class TitleUser extends TitleUserModel {
	public static function getDetail($where) {
		// 获取文章详情
		$detail = parent::detail($where);
		if (empty($detail) || $detail['is_delete']) {
			throwError('很抱歉，当前题目不存在');
		}

		return $detail;
	}

	public function getList($filter) {

		$userId = UserService::getCurrentLoginUserId();

		// 获取列表数据
		return $this->with(['title'])->alias('u')
			->join('learn_book b', 'b.subject_id=u.article_id')
			->where($filter)
			->where('u.user_id', '=', $userId)
			->field('u.*')
			->select()->toArray();

	}
/**
 * 获取复习条文列表
 * @param int $categoryId
 * @param int $limit
 * @return \think\Paginator
 * @throws \think\db\exception\DbException
 */
	public function getReviewList(int $bookId = 0, int $limit = 15): \think\Paginator {
		$userId = UserService::getCurrentLoginUserId();

		// 检索查询条件
		$filter = [];
		$filter[] = ['book_id', '=', $bookId];
		// 获取列表数据
		return $this->alias('u')
			->join('learn_article_title t', 't.article_id=u.article_id')

			->where($filter)
			->where('user_id', '=', $userId)
			->where('u.review_num', '>', 0)

			->field('t.book_name,t.book_name2,t.content,t.article_id,u.log_id')
			->order('u.review_num desc,u.create_time desc')
			->paginate($limit);

	}

	public function answer_save($userId, $data) {
		$book = Db::name('learn_book')
			->where('subject_id', $data['article_id'])->find();
		$total_num = $this['total_num'] + 1;
		$book_total_num = $book['total_num'] + 1;
		$time = $this['time'] + $data['time'];

		if ($data['is_right'] == 2) {
			$right_num = $this['right_num'];

			$book_right_num = $book['right_num'];

		} else {

			$right_num = $this['right_num'] + 1;

			$book_right_num = $book['right_num'] + 1;
		}

		$this->where('user_id', $userId)->where('article_id', $data['article_id'])
			->update(['right_num' => $right_num, 'total_num' => $total_num, 'time' => $time,
				'is_right' => $data['is_right'], 'answer' => $data['answer']]);

		Db::name('learn_book')
			->where('subject_id', $data['article_id'])
			->update(['right_num' => $book_right_num, 'total_num' => $book_total_num]);

		//统计

		$info = [];
		$info['right_num'] = $right_num;
		$info['total_num'] = $total_num;

		$percent = $right_num / $total_num * 100;

		$info['right_percent'] = sprintf('%.2f', $percent);

		$info['book_total_num'] = $book_total_num;
		$info['book_right_num'] = $book_right_num;

		$percent = $book_right_num / $book_total_num * 100;

		$info['book_right_percent'] = sprintf('%.2f', $percent);

		$info['like'] = $book['like'];

		//打卡

		$model = new ClockModel;
		$model->add($this['user_id'], $data['time']);
		return $info;
	}

	public function collectSave($userId, $articleId, $bookId) {
		$is_collect = Db::name('learn_book_user_collect')->where('user_id', $userId)
			->where('article_id', $articleId)->find();

		if (!$is_collect) {
			$data['book_id'] = $bookId;
			$data['user_id'] = $userId;
			$data['article_id'] = $articleId;
			$data['create_time'] = time();
			$data['store_id'] = self::$storeId;
			Db::name('learn_book_user_collect')->insert($data);
			$this->where('user_id', $userId)->where('article_id', $articleId)->update(['is_collect' => 1]);
			Db::name('learn_book')->where('subject_id', $articleId)->inc('like', 1)->update();
		} else {
			$this->where('user_id', $userId)->where('article_id', $articleId)->update(['is_collect' => 0]);
			Db::name('learn_book_user_collect')->where('user_id', $userId)
				->where('book_id', $bookId)->where('article_id', $articleId)->delete();
			Db::name('learn_book')->where('subject_id', $articleId)->dec('like', 1)->update();
		}

		return true;
	}
}